package de.haw.algorithm;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.RandomAccessFile;

public class ReadingThread extends Thread {

	private boolean atEnd = false;

	private final DatenContainer container = new DatenContainer();

	private final RandomAccessFile br;

	public ReadingThread(File file) throws FileNotFoundException {
		br = new RandomAccessFile(file, "rw");
	}

	public int[] getElemt() throws InterruptedException {
		return container.raus();
	}

	public boolean isAtEnd() {
		return atEnd && container.size() < 1;
	}

	@Override
	public void run() {
		try {
			byte[] zuspeichernByte = new byte[16000];
			int i = 0;
			while (i != -1) {
				i = br.read(zuspeichernByte);
				int[] zahlenSammlung = new int[(i / 4)];
				for (int j = 0; j < (i / 4); j++) {
					zahlenSammlung[j] = (zuspeichernByte[j * 4 + 0] << 24)
							| (zuspeichernByte[j * 4 + 1] << 16)
							+ (zuspeichernByte[j * 4 + 2] << 8)
							+ zuspeichernByte[j * 4 + 3];
				}
				container.rein(zahlenSammlung);
			}
			atEnd = true;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			interrupt();
		}
	}

}
